package com.myjianzhu.zzt88.exercise.android.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.myjianzhu.zzt88.exercise.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LineChartView extends View {
    private static final String TAG = LineChartView.class.getSimpleName();
    private int XPoint = 150;
    private int YPoint = 260;

    private int XLength = 380;
    private int YLength = 240;

    private int MaxDataSize;

    private List<Float> data = new ArrayList<>();
    private List<String> YLabel = new ArrayList<>();
    private List<String> XLabel = new ArrayList<>();
    private String unitLabel;
    private float textSize;
    private int XScale;  //刻度长度
    private int YSize;
    private int YScale;
    private int lineColor;
    private int lineGuidesColor;
    private int coordinateColor;


    public LineChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        simulationData();
        initData();
        init(context, attrs);

    }

    public LineChartView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.ChartView);

        textSize = context.getResources().getDimension(R.dimen.text_size_3);

        lineColor = context.getResources().getColor(android.R.color.black);
        lineGuidesColor = context.getResources().getColor(android.R.color.black);
        coordinateColor = context.getResources().getColor(android.R.color.black);

        lineColor = t.getColor(R.styleable.ChartView_lineColor, lineColor);
        lineGuidesColor = t.getColor(R.styleable.ChartView_lineGuidesColor, lineGuidesColor);
        coordinateColor = t.getColor(R.styleable.ChartView_coordinateColor, coordinateColor);
    }

    private void simulationData() {
        for (int i = 0; i < 6; i++) {
            YLabel.add("Y" + i);
        }
        Random random = new Random();
        for (int j = 0; j < 6; j++) {
            data.add(random.nextFloat() * 6);
        }
        for (int i = 0; i < 6; i++) {
            XLabel.add("X" + i);
        }
    }

    private void initData() {
        MaxDataSize = data.size();
        YSize = YLabel.size();
        YScale = YLength / YSize;
    }

    @Override
    public void invalidateOutline() {
        initData();
        super.invalidateOutline();
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        XPoint = w / 8;
        YPoint = h - 100;
        YLength = YPoint - 20;
        XLength = w / 16 * 13;
        MaxDataSize = data.size();
        YSize = YLabel.size();
        XScale = XLength / MaxDataSize;  //刻度长度
        YScale = YLength / YSize;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d(TAG, "onMeasure");
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d(TAG,"onLayout");
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG,"onDraw");

        Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setColor(lineColor);
        linePaint.setStrokeWidth(4F);

        Paint guidesPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        guidesPaint.setStyle(Paint.Style.STROKE);
        guidesPaint.setColor(lineGuidesColor);
        guidesPaint.setStrokeWidth(1F);

        Paint coordinatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        coordinatePaint.setStyle(Paint.Style.STROKE);
        coordinatePaint.setColor(coordinateColor);
        coordinatePaint.setStrokeWidth(4F);

        TextPaint textPaint = new TextPaint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(textSize);

        Paint pointPaint = new Paint();
        pointPaint.setColor(Color.WHITE);

        //画Y轴 
        canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, coordinatePaint);

        //Y轴箭头 
        canvas.drawLine(XPoint, YPoint - YLength, XPoint - 5, YPoint - YLength + 10, coordinatePaint);  //箭头
        textPaint.setTextAlign(Paint.Align.RIGHT);
        if (!TextUtils.isEmpty(unitLabel))
            canvas.drawText(unitLabel, XPoint - 20, YPoint - YLength + 50, textPaint);//单位文字
        canvas.drawLine(XPoint, YPoint - YLength, XPoint + 5, YPoint - YLength + 10, coordinatePaint);

        //添加刻度和文字 
        for (int i = 0; i < YLabel.size(); i++) {
            textPaint.setTextAlign(Paint.Align.RIGHT);
            canvas.drawLine(XPoint, YPoint - i * YScale, XPoint + XLength, YPoint - i * YScale, guidesPaint);  //刻度
            canvas.drawText(YLabel.get(i), XPoint - 20, YPoint - i * YScale, textPaint);//文字
        }

        //画X轴
        canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, coordinatePaint);

        //X轴箭头
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 10, YPoint + 5, coordinatePaint);  //箭头
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 10, YPoint - 5, coordinatePaint);

        for (int j = 0; j < XLabel.size(); j++) {
            if (XLabel.size() > 20 && XLabel.size() < 30 && j < XLabel.size() - 1) {
                if (j % 2 != 0) continue;
            }
            if (XLabel.size() >= 30 && j < XLabel.size() - 1) {
                if (j % 3 != 0) continue;
            }
            textPaint.setTextAlign(Paint.Align.CENTER);
            canvas.drawLine(XPoint + j * XScale, YPoint, XPoint + j * XScale, YPoint - 10, coordinatePaint);  //刻度
            canvas.drawText(XLabel.get(j), XPoint + j * XScale, YPoint + 50, textPaint);//文字
        }

        if (data.size() > 1) {
            canvas.drawCircle(XPoint, YPoint - data.get(0) * YScale, 4.0f, pointPaint);
            for (int i = 1; i < data.size(); i++) {
                canvas.drawLine(XPoint + (i - 1) * XScale, YPoint - data.get(i - 1) * YScale,
                        XPoint + i * XScale, YPoint - data.get(i) * YScale, linePaint);
                if (data.size() > 20 && data.size() < 30 && i < data.size() - 1) {
                    if (i % 2 != 0) continue;
                }
                if (data.size() >= 30 && i < data.size() - 1) {
                    if (i % 3 != 0) continue;
                }
                canvas.drawCircle(XPoint + i * XScale, YPoint - data.get(i) * YScale, 8.0f, pointPaint);
            }
        }

    }

    public LineChartView setXLabel(List<String> xLabel) {
        this.XLabel = xLabel;
        return this;
    }

    public LineChartView setYLabel(List<String> yLabel) {
        this.YLabel = yLabel;
        return this;
    }

    public LineChartView setData(List<Float> data) {
        this.data = data;
        MaxDataSize = data.size();
        YSize = YLabel.size();
        XScale = XLength / MaxDataSize;  //刻度长度
        YScale = YLength / YSize;
        return this;
    }

    public void setUnitLabel(String unitLabel) {
        this.unitLabel = unitLabel;
    }
}